package com.cius.eshop.controller;

import com.cius.eshop.bean.User;
import com.cius.eshop.bean.UserExample;
import com.cius.eshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ciuscheung on 17/7/11.
 */

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    //进入注册页面
    @RequestMapping(value = "/toRegister")
    public String toRegister(){
        return "personal/register";
    }

    //进入登录界面
    @RequestMapping(value = "/toLogin")
    public String toLogin(){
        return "personal/login";
    }

    //注册
    @RequestMapping(value = "/insertUser",method = {RequestMethod.POST})
    public String insertUser(Model model, User user){
        userService.insert(user);
        return "personal/login";
    }

    //登录
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Model model, String username, String password, HttpServletRequest request, HttpServletResponse response){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<User> users = userService.selectByExample(userExample);
        if(users.size() == 0){
            model.addAttribute("errors","您输入的用户名或密码不正确,请重新输入");
            return "personal/login";
        }
        else {
            model.addAttribute("user",users.get(0));
            return "index";
        }
    }

    //进入用户详细页面
    @RequestMapping(value = "toUserDetail")
    public String toUserDetail(Model model,String id){
        if(id == null)
            return "personal/login";
        else {
            User user = getUserById(Integer.parseInt(id));
            model.addAttribute("user",user);
            return "personal/userDetails";
        }
    }

    //根据id获得user
    public User getUserById(int id){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<User> users = userService.selectByExample(userExample);
        User user = userService.selectByPrimaryKey(id);
        return user;
    }

    @RequestMapping(value="/checkOldPassword")
    @ResponseBody
    public Map<String,Object> checkOldPassword(String userId,String oldPassword,HttpServletRequest request,
                                               HttpServletResponse response) throws IOException{
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andPasswordEqualTo(oldPassword);
        criteria.andIdEqualTo(Integer.parseInt(userId));
        List<User> users = userService.selectByExample(userExample);

        System.out.println(request.getParameter("name"));
        Map<String,Object> map = new HashMap<String,Object>();

        if(users.size() != 0){
            System.out.println("城东");
            map.put("msg", "成功");
        }else{
            System.out.println("失败");
            map.put("msg", "失败");
        }
        return map;
    }

    @RequestMapping("/toChangePass")
    public String toChangePass(Model model,String id){
        User user = userService.selectByPrimaryKey(Integer.parseInt(id));
        model.addAttribute("user",user);
        return "personal/changePass";
    }


    @RequestMapping(value = "modifyUser")
    public String modifyUser(Model model,User user){
        userService.updateByPrimaryKeySelective(user);
        model.addAttribute("user",user);
        return "personal/userDetails";
    }

    @RequestMapping(value = "/checkUserName",method = { RequestMethod.POST })
    @ResponseBody
    public Map<String,Object> checkUserName(@RequestBody User user){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        List<User> users = userService.selectByExample(userExample);
        Map<String,Object> map = new HashMap<>();
        map.put("username",users.get(0).getUsername());
        return map;
    }

}
